package com.ayoka.parenting.ui.coach.studentdetail;

import android.app.Activity;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.ayoka.parenting.R;
import com.ayoka.parenting.adapter.GrownPathAdapter;
import com.ayoka.parenting.bean.GrownPathBean;
import com.ayoka.parenting.bean.PagerBean;
import com.ayoka.parenting.bean.UserInfoBean;
import com.ayoka.parenting.bean.UserInfoDetailBean;
import com.ayoka.parenting.databinding.ActivityBaseRecyclerviewBinding;
import com.ayoka.parenting.databinding.HeaderStudentInfoBinding;
import com.ayoka.parenting.mvp.MVPBaseRecyclerViewActivity;
import com.jincp.common.brvah.BaseQuickAdapter;
import com.jincp.common.brvah.BaseViewHolder;
import com.jincp.common.brvah.item_decoration.TimeLineDecoration;
import com.jincp.common.model.ToolBarOptions;
import com.jincp.common.utils.UIUtils;
import com.jincp.image.load.ImageLoader;
import com.jincp.image.load.ImageLoaderOptions;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: StudentDetailActivity.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class StudentDetailActivity extends MVPBaseRecyclerViewActivity<GrownPathBean, ActivityBaseRecyclerviewBinding, StudentDetailContract.View, StudentDetailPresenter> implements StudentDetailContract.View {

    public static void start(Activity activity, String  studentId) {
        Intent intent = new Intent(activity, StudentDetailActivity.class);
        intent.putExtra("StudentId", studentId);
        activity.startActivity(intent);
    }

    private HeaderStudentInfoBinding headerBinding;

    private TimeLineDecoration timeLineDecoration;

    private String studentId;

    private UserInfoDetailBean studentBean;

    @Override
    protected void configIntentValue() {
        Intent intent = getIntent();
        if (intent != null) {
            studentId = intent.getStringExtra("StudentId");
        }
    }

    @Override
    public void initViews() {
        super.initViews();
        headerBinding = DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.header_student_info, null, false);
        adapter.addHeaderView(headerBinding.getRoot());
        adapter.setHeaderAndEmpty(true);
    }


    @Override
    protected SwipeRefreshLayout getRefreshLayout() {
        return mDataBinding.swipeRefresh;
    }

    @Override
    protected RecyclerView getRecyclerView() {
        mDataBinding.recyclerView.setBackgroundColor(Color.WHITE);
        return mDataBinding.recyclerView;
    }

    @NonNull
    @Override
    protected BaseQuickAdapter<GrownPathBean, BaseViewHolder> getAdapter() {
        GrownPathAdapter adapter = new GrownPathAdapter();
        return adapter;
    }

    @Override
    protected RecyclerView.ItemDecoration getRecycleDivider() {
        return new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                int position = parent.getChildAdapterPosition(view);
                int count = parent.getAdapter().getItemCount();
                if (position > 0 && position < count - 1) {
                    outRect.right = UIUtils.dp2px(15);
                }
            }
        };
    }

    @Override
    protected void getData(boolean isShowWaitDialog) {
        if (mPresenter != null) {
            if (studentBean == null) {
                mPresenter.getStudentData(studentId);
            }
            mPresenter.getStudentGrowPathListData(isShowWaitDialog, studentId, pageNo, pageSize);
        }
    }


    @Override
    protected void initData(Bundle savedInstanceState) {
        ToolBarOptions options = new ToolBarOptions(R.string.student_information);
        setCustomToolBar(mDataBinding.customToolbar.toolbar, options);

        getData(true);
    }

    @Override
    protected StudentDetailPresenter getPresenter() {
        return new StudentDetailPresenter();
    }

    @Override
    public void configStudentData(UserInfoDetailBean data) {
        if (data == null) return;
        this.studentBean = data;
        if (studentBean.user_info != null) {
            if (studentBean.user_info.BasicInfo != null) {
                ImageLoader.getInstance().displayImage(studentBean.user_info.BasicInfo.LocalAvatar, headerBinding.ivUserHeader, ImageLoaderOptions.getCircleOptions());
                headerBinding.tvUserName.setText(studentBean.user_info.BasicInfo.WeixinNickName);
            }
            // 设置冥想数据
            int totalMeditation = Math.round(studentBean.user_info.MeditationTotalTime / 60);
            headerBinding.tvTotalMeditation.setText(String.valueOf(totalMeditation));
            int todayMeditation = Math.round(studentBean.user_info.MeditationTodayTime / 60);
            headerBinding.tvTodayMeditation.setText(String.valueOf(todayMeditation));
            headerBinding.tvTotalMeditationDay.setText(String.valueOf(studentBean.user_info.MeditationTotalDays));
        }
    }

    @Override
    public void configStudentGrowPathListData(PagerBean<GrownPathBean> data) {
        if (data == null) return;
        setLoadMoreLayout(data.list, data.totalPage);
        if (timeLineDecoration == null) {
            timeLineDecoration = new TimeLineDecoration(this, 0, 1, 0);
            timeLineDecoration.setCircleSize(10);
            timeLineDecoration.setOffsetX(50);
            mDataBinding.recyclerView.addItemDecoration(timeLineDecoration);
        }
    }
}
